package com.link510.aitools.activity.airobot;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.google.common.collect.Lists;
import com.link510.aitools.R;
import com.link510.aitools.activity.CommandActivity;
import com.link510.aitools.activity.Login2Activity;
import com.link510.aitools.activity.airobot.camera.RobotCameraActivity;
import com.link510.aitools.activity.airobot.core.RobotCoreActivity;
import com.link510.aitools.activity.airobot.led.RobotLedActivity;
import com.link510.aitools.activity.airobot.audio.RobotAudioActivity;
import com.link510.aitools.activity.airobot.picture.RobotPictureActivity;
import com.link510.aitools.core.domain.ActionInfo;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.framework.adapter.HomeActionAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RobotMainActivity extends BaseAcitivity {


    private CountDownTimer countDownTimer;

    private HomeActionAdapter homeActionAdapter = null;

    private List<ActionInfo> homeActionInfoList = null;

    private boolean isExit = false;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                isExit = false;
            }
        }
    };


    @BindView(R.id.home_action_listview)
    GridView homeListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot_main);
        initData();
    }

    private void initData() {

        homeActionAdapter = new HomeActionAdapter(this.mContext) {
            @Override
            public List<ActionInfo> getActionInfoList() {

                if (homeActionInfoList == null) {
                    synchronized (RobotMainActivity.class) {
                        if (homeActionInfoList != null) {
                            return homeActionInfoList;
                        }
                        homeActionInfoList = Lists.newArrayList();
                        homeActionInfoList.add(new ActionInfo("core", R.string.action_core, R.mipmap.action_core));
                        homeActionInfoList.add(new ActionInfo("camera", R.string.action_camera, R.mipmap.action_camera));
                        homeActionInfoList.add(new ActionInfo("led", R.string.action_led, R.mipmap.action_led));
                        homeActionInfoList.add(new ActionInfo("audio", R.string.action_audio, R.mipmap.action_audio));
                        homeActionInfoList.add(new ActionInfo("command", R.string.action_command, R.mipmap.action_command));
                    }
                }

                return homeActionInfoList;
            }

            @Override
            public void toAction(ActionInfo actionInfo) {
                RobotMainActivity.this.toAction(actionInfo);
            }
        };


        homeListView.setAdapter(homeActionAdapter);
    }

    private void toAction(ActionInfo actionInfo) {

        if (actionInfo == null) {
            ToastUtils.showShort("异常的操作");
            return;
        }

        switch (actionInfo.getAction()) {
            case "core": {
                Intent intent = new Intent(RobotMainActivity.this, RobotCoreActivity.class);
                startActivity(intent);
                break;
            }
            case "camera": {
                Intent intent = new Intent(RobotMainActivity.this, RobotCameraActivity.class);
                startActivity(intent);
                break;
            }
            case "led": {
                Intent intent = new Intent(RobotMainActivity.this, RobotLedActivity.class);
                startActivity(intent);
                break;
            }
            case "audio": {
                Intent intent = new Intent(RobotMainActivity.this, RobotAudioActivity.class);
                startActivity(intent);
                break;
            }
            case "command": {
                Intent intent = new Intent(RobotMainActivity.this, CommandActivity.class);
                startActivity(intent);
                break;
            }
            default: {
                ToastUtils.showShort("异常的操作");
            }
        }

    }

    /**
     * 监听返回键
     *
     * @param keyCode 键值
     * @param event   事件
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if (isExit) {
                mHandler.removeCallbacksAndMessages(null);
            } else {
                isExit = true;
                mHandler.sendEmptyMessageDelayed(0, 3000);
                ToastUtils.showShort("再按一次退出");
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

//    /**
//     * 核心板点击
//     */
//    @OnClick(R.id.coreboard_button)
//    public void coreboardButtonClick() {
//        Intent intent = new Intent(RobotMainActivity.this, RobotCoreActivity.class);
//        startActivity(intent);
//    }
//
//    /**
//     * 核心板点击
//     */
//    @OnClick(R.id.camera_button)
//    public void cameraButtonClick() {
//        Intent intent = new Intent(RobotMainActivity.this, RobotCameraActivity.class);
//        startActivity(intent);
//    }
//
//    /**
//     * 核心板点击
//     */
//    @OnClick(R.id.led_button)
//    public void ledButtonClick() {
//        Intent intent = new Intent(RobotMainActivity.this, RobotLedActivity.class);
//        startActivity(intent);
//    }
//
//    /**
//     * 核心板点击
//     */
//    @OnClick(R.id.audio_button)
//    public void audioButtonClick() {
//        Intent intent = new Intent(RobotMainActivity.this, RobotSoundActivity.class);
//        startActivity(intent);
//    }
//
//
//    /**
//     * 核心板点击
//     */
//    @OnClick(R.id.command_button)
//    public void commandButtonClick() {
//        Intent intent = new Intent(RobotMainActivity.this, CommandActivity.class);
//        startActivity(intent);
//    }
//

    /**
     * 图片文件点击
     */
    @OnClick(R.id.picture_file_button)
    public void pictureButtonClick() {
        Intent intent = new Intent(RobotMainActivity.this, RobotPictureActivity.class);
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.robot_main_right_topmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // 菜单的监听方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.select_device_item) {
            Intent intent = new Intent(RobotMainActivity.this, Login2Activity.class);
            startActivity(intent);
            finish();
        }

        return true;
    }

}